package com.fyx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fyx.annotation.Log;
import com.fyx.entity.*;
import com.fyx.modules.system.service.DataService;
import com.fyx.modules.system.service.DeptService;
import com.fyx.modules.system.service.UserService;
import com.fyx.service.IPayService;
import com.fyx.utils.PageUtil;
import com.fyx.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags = "付费留存")
@RestController
@RequestMapping("/api/pay")
@RequiredArgsConstructor
public class PayController {

    @Autowired
    private IPayService iPayService;

    @Log("查询推广地址管理")
    @ApiOperation("查询推广地址管理")
    @GetMapping(value = "address")
    @PreAuthorize("@el.check('address:list')")
    public ResponseEntity<Object> address(SysUser sysUser, Pageable pageable){
        Map mapResult = iPayService.address(sysUser,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    private final DeptService deptService;
    private final DataService dataService;
    private final UserService userService;

    @Log("查询苹果补单列表")
    @ApiOperation("查询苹果补单列表")
    @GetMapping(value="abnormalOrders")
    @PreAuthorize("@el.check('abnormalOrders:list')")
    public Map abnormalOrders(AbnormalOrdersEntity abnormalOrdersEntity, Pageable pageable){

        Map mapResult = iPayService.abnormalOrders(abnormalOrdersEntity,pageable);
        return  mapResult;
    }

    @Log("查询超时补单列表")
    @ApiOperation("查询超时补单列表")
    @GetMapping(value="abnormalOrders1")
    @PreAuthorize("@el.check('abnormalOrders:list')")
    public Map abnormalOrders1(AbnormalOrdersEntity abnormalOrdersEntity, Pageable pageable){
        Map mapResult = iPayService.abnormalOrders1(abnormalOrdersEntity,pageable);
        return  mapResult;
    }

    @Log("苹果补单")
    @ApiOperation("苹果补单")
    @PostMapping(value = "appleBudan")
//    @PreAuthorize("@el.check('toCheck:checkPass')")
    public Boolean appleBudan(@Validated @RequestBody AbnormalOrdersEntity abnormalOrdersEntity) throws IOException {
        String str = iPayService.appleBudan(abnormalOrdersEntity);
        if(str.equals("false")){
            return  false;
        }else {
            return true;
        }
    }

    @Log("超时补单")
    @ApiOperation("超时补单")
    @PostMapping(value = "timeOutBudan")
//    @PreAuthorize("@el.check('toCheck:checkPass')")
    public Map timeOutBudan(@Validated @RequestBody AbnormalOrdersEntity abnormalOrdersEntity) throws IOException {
        String str = iPayService.timeOutBudan(abnormalOrdersEntity);
        Map resultMap = (Map) JSONObject.parse(str);
        return  resultMap;
    }

    @Log("查询充值分类统计")
    @ApiOperation("查询充值分类统计")
    @GetMapping(value="rechargeCount")
    @PreAuthorize("@el.check('rechargeCount:list')")
    public Map rechargeCount(OrderEntity orderEntity, Pageable pageable){
        Map mapResult = iPayService.rechargeCount(orderEntity,pageable);
        return mapResult;
    }

    @Log("查询充值记录")
    @ApiOperation("查询充值记录")
    @GetMapping(value = "rechargeRecord")
    @PreAuthorize("@el.check('rechargeRecord:list')")
    public ResponseEntity<Object> rechargeRecord(OrderEntity orderEntity, Pageable pageable){
        Map mapResult = iPayService.rechargeRecord(orderEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);

    }

    @Log("查询玩家订单")
    @ApiOperation("查询玩家订单")
    @GetMapping(value="queryGameUserOrder")
    @PreAuthorize("@el.check('queryGameUserOrder:list')")
    public ResponseEntity<Object> queryGameUserOrder(OrderEntity orderEntity, Pageable pageable){
        // 数据权限
        if (!ObjectUtils.isEmpty(orderEntity.getDeptId())) {
            orderEntity.getDeptIds().add(orderEntity.getDeptId());
            orderEntity.getDeptIds().addAll(deptService.getDeptChildren(deptService.findByPid(orderEntity.getDeptId())));
        }
        List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));

        if (!CollectionUtils.isEmpty(orderEntity.getDeptIds()) && !CollectionUtils.isEmpty(dataScopes)){
            // 取交集
            orderEntity.getDeptIds().retainAll(dataScopes);
        } else {
            // 否则取并集
            orderEntity.getDeptIds().addAll(dataScopes);
        }
        Map mapResult = iPayService.queryGameUserOrder(orderEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);

    }

    @Log("查询充值统计")
    @ApiOperation("查询充值统计")
    @GetMapping(value = "rechargeStatistics")
    @PreAuthorize("@el.check('rechargeStatistics:list')")
    public Map getChannelRechargeCountList(ChannelDataEntity channelDataEntity, Pageable pageable){
        Map mapResult = iPayService.rechargeStatistics(channelDataEntity, pageable);
        return mapResult;
    }

    @Log("VIP用户统计")
    @ApiOperation("VIP用户统计")
    @GetMapping(value="getNobleVip")
    @PreAuthorize("@el.check('getNobleVip:list')")
    public Map getNobleVip(LostGold lostGold, Pageable pageable){
        Map mapResult = iPayService.getNobleVip(lostGold,pageable);
        return mapResult;
    }

    @PostMapping(value = "getNobleVipTree")
    @PreAuthorize("@el.check('getNobleVip:list')")
    public List<MatchWinEntity> getNobleVipTree(@Validated @RequestBody JSONObject jsonObject){
        String channel = jsonObject.getString("channel");
        Integer time = Integer.valueOf(jsonObject.getString("time")) ;
        String flag = jsonObject.getString("flag");
        List<MatchWinEntity> list = iPayService.getNobleVipTree(time,channel,flag);
        return list;
    }
}
